package com.yunfei.api.v2.service;

import com.yunfei.api.v2.param.AddElecContCustAddInfoParam;
import com.yunfei.api.v2.param.CreatePersonInfoParam;
import com.yunfei.api.v2.result.addAccountInfo.AddAccountInfoResultModel;
import com.yunfei.api.v2.result.createPersonInfo.CreatePersonInfoJsonRootBean;
import com.yunfei.api.v2.result.tunRegister.TurnRegisterResult;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.ResultStatus;
import com.yunfei.mapper.aiiidb.ElecContCustAddInfoMapper;
import com.yunfei.mapper.aiiidb.HeTongSignInfoMapper;
import com.yunfei.mapper.crm.TunRegisterMapper;
import com.yunfei.utils.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @Description:添加签署者
* @Author: zhengyunfei
* @Date: 2019/8/9
* @time:10:48
*/
@Service
public class CaRenZhengService {
    @Autowired
    private Environment env;
    @Autowired
    private GetLongTokenService getLongTokenService;
    @Autowired
    private MandatorIdService mandatorIdService;
    @Autowired
    private HeTongSignInfoMapper heTongSignInfoMapper;
    @Autowired
    private ElecContCustAddInfoMapper elecContCustAddInfoMapper;
    @Autowired
    private TunRegisterMapper tunRegisterMapper;
    @Autowired
    private CreatePersonInfoService createPersonInfoService;
    @Autowired
    private QueryUserNoService queryUserNoService;
    /**
     * @Description: 客户ca恩正
     * @param custNo 客户号
     * @param loginMobile 登录手机号
     * @param mobileNo 如果是银行卡四要素，此号码存储填写的验证的手机号
     * @return: java.lang.Object
     * @Author: zhengyunfei
     * @Date: 2019/11/11
     * @time:15:13
     */
    public Object caRenZheng(ResultModel resultModel, String custNo,String loginMobile,String mobileNo) {
        //验证客户对应的证件号是否已认证
        Map<String, Object> isRenZhengQueryMap = new HashMap<>();
        isRenZhengQueryMap.put("custNo", custNo);
        isRenZhengQueryMap.put("mobile", loginMobile);
        int isRenZheng = elecContCustAddInfoMapper.isRenZheng(isRenZhengQueryMap);
        //如果已认证，无需调用接口 （上述SQL>=1）
        //如果未认证，调用 创建个人用户 API接口，为客户进行认证。(上述SQL = 0)
        String signerId = "";//签署者id，用户id
        TurnRegisterResult turnRegisterResult = tunRegisterMapper.get(isRenZhengQueryMap);
        if(StringUtil.isNullOrEmpty(turnRegisterResult)){
            resultModel.setData("客户不存在");
            resultModel.setCode(ResultStatus.NOT_FOUND.getCode());
            resultModel.setMessage(ResultStatus.NOT_FOUND.getMessage());
            return resultModel;
        }
        if(StringUtil.isNullOrEmpty(mobileNo)){
            mobileNo=turnRegisterResult.getMobileNo();
        }
        if (isRenZheng == 0) {
            if (!StringUtil.isNullOrEmpty(turnRegisterResult)) {
                CreatePersonInfoParam createPersonInfoParam = new CreatePersonInfoParam();
                createPersonInfoParam.setCaType("B2");//证书类型
                createPersonInfoParam.setCertifyNum(turnRegisterResult.getIdentityNo());//身份证号码
                createPersonInfoParam.setCertifyType(turnRegisterResult.getIdentityType());//证件类型中文
                createPersonInfoParam.setIdentityRegion(turnRegisterResult.getCustAdd());//身份地区
                createPersonInfoParam.setPhoneNo(turnRegisterResult.getMobileNo());//手机号码
                createPersonInfoParam.setPhoneRegion(turnRegisterResult.getMobileAdd());//手机号地区
                createPersonInfoParam.setUserName(turnRegisterResult.getCustName());//企业名称
                CreatePersonInfoJsonRootBean createPersonInfoJsonRootBean = createPersonInfoService.createPersonInfo(createPersonInfoParam, "");
                if (createPersonInfoJsonRootBean.getCode() == 200) {
                    //插入表ELEC_CONT_CUST_ADD_INFO
                    signerId=createPersonInfoJsonRootBean.getData().getSignerId();
                    AddElecContCustAddInfoParam addElecContCustAddInfoParam = new AddElecContCustAddInfoParam();
                    addElecContCustAddInfoParam.setCaCustId(signerId);
                    addElecContCustAddInfoParam.setCertNo(turnRegisterResult.getIdentityNo());//证件号码
                    addElecContCustAddInfoParam.setCertTyp(turnRegisterResult.getIdentityType());//证件类型
                    addElecContCustAddInfoParam.setCustAdd(turnRegisterResult.getCustAdd());//身份地区
                    addElecContCustAddInfoParam.setCustId(custNo);//客户号
                    addElecContCustAddInfoParam.setCustNm(turnRegisterResult.getCustName());//客户名称
                    addElecContCustAddInfoParam.setCustType(turnRegisterResult.getCustType());//客户类型
                    addElecContCustAddInfoParam.setMobileAdd(turnRegisterResult.getMobileAdd());//手机号地区
                    addElecContCustAddInfoParam.setMobileNo(mobileNo);//手机号码
                    Map<String,Object> queryMap=new HashMap<>();
                    queryMap.put("custId",custNo);
                    queryMap.put("certNo",turnRegisterResult.getIdentityNo());
                    int count=elecContCustAddInfoMapper.count(queryMap);
                    if(count==0){
                        elecContCustAddInfoMapper.add(addElecContCustAddInfoParam);
                    }

                } else {
                    return createPersonInfoJsonRootBean;
                }
            }
        } else {
            //已认证，无需调用创建个人用户的接口，需要插入ELEC_CONT_CUST_ADD_INFO
            //根据证件号码查询签署者id
            String certifyNum = turnRegisterResult.getIdentityNo();
            signerId = queryUserNoService.queryUserNo(certifyNum, "");
            AddElecContCustAddInfoParam addElecContCustAddInfoParam = new AddElecContCustAddInfoParam();
            addElecContCustAddInfoParam.setCaCustId(signerId);
            addElecContCustAddInfoParam.setCertNo(turnRegisterResult.getIdentityNo());//证件号码
            addElecContCustAddInfoParam.setCertTyp(turnRegisterResult.getIdentityType());//证件类型
            addElecContCustAddInfoParam.setCustAdd(turnRegisterResult.getCustAdd());//身份地区
            addElecContCustAddInfoParam.setCustId(custNo);//客户号
            addElecContCustAddInfoParam.setCustNm(turnRegisterResult.getCustName());//客户名称
            addElecContCustAddInfoParam.setCustType(turnRegisterResult.getCustType());//客户类型
            addElecContCustAddInfoParam.setMobileAdd(turnRegisterResult.getMobileAdd());//手机号地区
            addElecContCustAddInfoParam.setMobileNo(mobileNo);//手机号码
            Map<String,Object> queryMap=new HashMap<>();
            queryMap.put("custId",custNo);
            queryMap.put("certNo",turnRegisterResult.getIdentityNo());
            int count=elecContCustAddInfoMapper.count(queryMap);
            if(count==0){
                elecContCustAddInfoMapper.add(addElecContCustAddInfoParam);
            }

        }
        //返回签署者签署者 ID
        AddAccountInfoResultModel addAccountInfoResultModel = new AddAccountInfoResultModel();
        addAccountInfoResultModel.setSignerId(signerId);
        resultModel.setData(addAccountInfoResultModel);
        return resultModel;
    }

}
